<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

if (!function_exists('my_permissios')) {

    function my_permissios() {
        $CI = & get_instance();
        $user_purview = $CI->login_lib->purview();
        if ($user_purview != "#all") {
            $segs = $CI->uri->segment_array();
            $tempDir = array();
            $i = 1;
            $j = 1;
            for (; $i < count($segs); $i++) {
                $tempDir[] = $segs[$i];
                $j = $i + 1;
                if (!is_dir(APPPATH . 'controllers/' . implode('/', $tempDir))) {
                    $j--;
                    break;
                }
            }
            if (isset($segs[$j + 1]))
                $per = "," . $segs[$j] . "_" . $segs[$j + 1] . ",";
            else {
                $per = "," . $segs[$j] . "_index,";
            }
            if (!strstr($user_purview, $per)) {
                //echo $per;
                //echo $user_purview;
                //redirect("/access_control");
            }
        }
    }

}

if (!function_exists('my_menu')) {

    function my_menu() {
        $CI = & get_instance();
        $segs = $CI->uri->segment_array();
        $tempDir = array();
        $i = 1;
        $j = 1;
        for (; $i < count($segs); $i++) {
            $tempDir[] = $segs[$i];
            $j = $i + 1;
            if (!is_dir(APPPATH . 'controllers/' . implode('/', $tempDir))) {
                $j--;
                break;
            }
        }
        return $segs[$j];
    }

}

//if (!function_exists('check_menu_permissios')) {
if (!function_exists('check_broker_is_visible')) {

    function check_broker_is_visible() {
        $CI = & get_instance();
        $user_purview = $CI->login_lib->purview();
        if (strstr($user_purview, "broker_is_visible")) {
            return true;
        } elseif ($user_purview == "#all") {
            return true;
        } else {
            return false;
        }
    }

}

if (!function_exists('check_menu_permissios')) {

    function check_menu_permissios($per) {
        $CI = & get_instance();
        $user_purview = $CI->login_lib->purview();
        if ($user_purview != "#all") {
            if (strstr($user_purview, "," . $per . ","))
                return true;
            else
                return false;
        }else {
            return true;
        }
    }

}


if (!function_exists('check_top_menu_permissios')) {

    function check_top_menu_permissios($per) {
        $CI = & get_instance();
        $user_purview = $CI->login_lib->purview();
        if ($user_purview != "#all") {
            if (strstr($user_purview, "," . $per . "_"))
                return true;
            else
                return false;
        }else {
            return true;
        }
    }

}

if (!function_exists('get_all_permissios')) {

    function get_all_permissios($dir = "") {
        static $permissios_array = array();
        if ($dir != "")
            $dir.="/";
        $path = APPPATH . 'controllers/admin/' . $dir;
        if (is_dir($path)) {
            if ($handle = opendir($path)) {
                while (false !== ($file = readdir($handle))) {
                    if ($file != "." && $file != "..") {
                        if (is_file($path . $file)) {
                            $url = $path . $file;
                            $controllers_name = explode(".", $file);
                            $contents = file_get_contents($url);
                            preg_match_all('/public function(.*)\(/i', $contents, $array_c);
                            foreach ($array_c[1] as $key => $val) {
                                if (trim($val) != "__construct")
                                    $permissios_array[] = $controllers_name[0] . '_' . trim($val);
                            }
                        } elseif (is_dir($path . $file)) {
                            get_all_permissios($file);
                        }
                    }
                }
            }
            closedir($handle);
        }
        return $permissios_array;
    }

}